package vn.cuhanh.sys;


import vn.cuhanh.R;
import vn.cuhanh.utils.HelperFunc;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private Context context;
	
	public DBHelper(Context context) {		
		super(context, context.getResources().getString(R.string.database), null, context.getResources().getInteger(R.integer.database_version));
		this.context = context;
	}
	
	// Method is called during creation of the database
	@Override
	public void onCreate(SQLiteDatabase database) {
		String[] query = this.context.getResources().getStringArray(R.array.sql_create);
		for (int i = 0; i < query.length; i++)
		{
			try {
				database.execSQL(query[i]);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// Method is called during an upgrade of the database, e.g. if you increase
	// the database version
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion,	int newVersion) {
		HelperFunc.logDebug("DBHelper -> onUpgrade ->" + DBHelper.class.getName(), 
				"Upgrading database from version " + oldVersion + " to "
								+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS games");
		onCreate(database);
	}
}
